package it.appcatalogobase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;

public class CategoriaDataSource {
	 // Database fields
	  private SQLiteDatabase database;
	  private CatalogoOpenHelper dbHelper;
	  private String[] allColumns = { CatalogoOpenHelper.COLUMN_ID_CAT,
			  CatalogoOpenHelper.COLUMN_NAME_CAT, CatalogoOpenHelper.COLUMN_MOD_CAT, CatalogoOpenHelper.COLUMN_DES_CAT };

	  public CategoriaDataSource(Context context) {
	    dbHelper = new CatalogoOpenHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Categoria createCategoria(String categoria, String descrizione) {
	    ContentValues values = new ContentValues();
	    values.put(CatalogoOpenHelper.COLUMN_NAME_CAT, categoria);
	    values.put(CatalogoOpenHelper.COLUMN_DES_CAT, descrizione);
	    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	
		 // Get the date today using Calendar object.
		 Date today = Calendar.getInstance().getTime();        
		 // Using DateFormat format method we can create a string 
		 // representation of a date with the defined format.
		 String reportDate = df.format(today);
	    values.put(CatalogoOpenHelper.COLUMN_MOD_CAT, reportDate);
	    long insertId = database.insert(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, null,
	        values);
	    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
	        allColumns, CatalogoOpenHelper.COLUMN_ID_CAT + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Categoria newCategoria = cursorToCategoria(cursor);
	    cursor.close();
	    return newCategoria;
	  }
	  
	  public Categoria createCategoria(Long id, String categoria, String descrizione) {
		  	ContentValues values = new ContentValues();
		  	values.put(CatalogoOpenHelper.COLUMN_ID_CAT, id);
		    values.put(CatalogoOpenHelper.COLUMN_NAME_CAT, categoria);
		    values.put(CatalogoOpenHelper.COLUMN_DES_CAT, descrizione);
		    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    Date date = new Date();
		    values.put(CatalogoOpenHelper.COLUMN_MOD_CAT, dateFormat.format(date));
		    long insertId = database.insert(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, null,
		        values);
		    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
		        allColumns, CatalogoOpenHelper.COLUMN_ID_CAT + " = " + insertId, null,
		        null, null, null);
		    cursor.moveToFirst();
		    Categoria newCategoria = cursorToCategoria(cursor);
		    cursor.close();
		    return newCategoria;
		}

	  public void deleteCategoria(Categoria cat) {
	    long id = cat.getId();
	    System.out.println("Comment deleted with id: " + id);
	    database.delete(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, CatalogoOpenHelper.COLUMN_ID_CAT
	        + " = " + id, null);
	  }
	  
	  public void deleteCategorie() {
		    database.delete(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, null, null);
		  }

	  public List<Categoria> getAllCategorie() {
	    List<Categoria> categorie = new ArrayList<Categoria>();

	    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Categoria categoria = cursorToCategoria(cursor);
	      categorie.add(categoria);
	      cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return categorie;
	  }
	  
	  public Categoria getCategoria(Long id) {
//		    List<Categoria> categorie = new ArrayList<Categoria>();

		    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
		        allColumns, CatalogoOpenHelper.COLUMN_ID_CAT+"="+id, null, null, null, null);
		    if(cursor.getCount()>0){
			    cursor.moveToFirst();
	//		    while (!cursor.isAfterLast()) {
			      Categoria categoria = cursorToCategoria(cursor);
	//		      categorie.add(categoria);
			      cursor.moveToNext();
	//		    }
			    // make sure to close the cursor
			    cursor.close();
			    return categoria;
		    }else
		    	return null;
		  }

	  private Categoria cursorToCategoria(Cursor cursor) {
	    Categoria categoria = new Categoria();
	    categoria.setId(cursor.getLong(0));
	    categoria.setNome(cursor.getString(1));
	    categoria.setDataMod(cursor.getString(2));
	    categoria.setDescrizione(cursor.getString(3));
	    return categoria;
	  }

	public void deleteCategoriaFromId(Long id) {
	    database.delete(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, CatalogoOpenHelper.COLUMN_ID_CAT+"="+id, null);
	}

	
}
